package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.modules.app.dao.AlipayNotifyDao;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.entity.AlipayNotifyEntity;
import io.renren.modules.app.entity.OrderEntity;
import io.renren.modules.app.service.AlipayNotifyService;
import io.renren.modules.app.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther zxl
 * @Date 2018/12/18 16:57
 * @Description
 **/
@Service("AlipayNotifyService")
public class AlipayNotifyServiceImpl extends ServiceImpl<AlipayNotifyDao, AlipayNotifyEntity>
        implements AlipayNotifyService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    OrderService orderService;

    @Autowired
    AlipayNotifyDao alipayNotifyDao ;
    @Autowired
    private UserDao userDao;

    @Override
    public boolean insertOne(AlipayNotifyEntity alipayNotifyEntity) {

        return this.insert(alipayNotifyEntity);
    }

    @Override
    public boolean alipayReturn(AlipayNotifyEntity alipayNotifyEntity) {


        Integer res = alipayNotifyDao.updateWhenReturn(alipayNotifyEntity);
        if (res>0){
            // 更新订单状态
            orderService.updateOrderState(alipayNotifyEntity.getOrderId(),2);
            return true;
        }
        logger.error("return service fails");

        return false;
    }

    @Override
    public boolean alipayNotify(AlipayNotifyEntity alipayNotifyEntity) {

        Integer res = alipayNotifyDao.updateWhenNotify(alipayNotifyEntity);

        if (res>0){
            OrderEntity orderEntity = orderService.getOneByOrderId(alipayNotifyEntity.getOrderId());
            if (StringUtils.equals("TRADE_SUCCESS",alipayNotifyEntity.getTradeStatus())){
                userDao.updateUserState(4,orderEntity.getUserId());
            }else {
                userDao.updateUserState(3,orderEntity.getUserId());
                // 支付失败
                orderService.updateOrderState(orderEntity.getOrderId(),11);
            }
            return true;
        }
        logger.error("notify service fails");

        return false;
    }

    @Override
    public AlipayNotifyEntity getOne(String orderId) {
        AlipayNotifyEntity alipayNotifyEntity = new AlipayNotifyEntity();
        alipayNotifyEntity.setOrderId(orderId);
        alipayNotifyEntity = baseMapper.selectOne(alipayNotifyEntity);
        System.out.println("-------------"+alipayNotifyEntity);
//        System.out.println(alipayNotifyEntity.toString());
        return alipayNotifyEntity;
//
//        return alipayNotifyDao.selectOneByOrderId(orderId);


    }

    @Override
    public boolean update(AlipayNotifyEntity alipayNotifyEntity) {

        return baseMapper.update(alipayNotifyEntity,
                new EntityWrapper<AlipayNotifyEntity>()
                        .where("order_id={0}",alipayNotifyEntity.getOrderId()))>0;
    }
}
